/* 
 * File:   Registration.cpp
 * Author: Allison
 * 
 * Created on September 30, 2013, 7:32 PM
 */

#include "Registration.h"

Registration::Registration() {
}

Registration::~Registration() {
}

int Registration::studentExists(string id) {
    int index = -1;
    if (studentList.size() > 0) {
        for (int i = 0; i < studentList.size(); i++) {
            Student s = studentList[i];
            if (s.getStudentID() == id)
                index = i;
        }
    }
    return index;
}

bool Registration::addStudent(string fName, string lName, string id, bool res, double r, double fee) {
    Student s = Student(fName, lName, id, res, r);
    int i = studentExists(s.getStudentID());
    if (i==-1) {
        studentList.push_back(s);
        return true;
    }
    else
        return false;
}

bool Registration::removeStudent(string id) {
    int i = studentExists(id);
    if (i == -1)
        return false;
    else {
        studentList.erase(studentList.begin()+i);
        return true;
    }
}

string Registration::listStudents() {
    string result = "";
    if(studentList.empty())
        return ("no student\n");
    else {
        for (int i=0; i<studentList.size(); i++) {
            Student s = studentList[i];
            result = result + s.toString();
        }
        return result;
    }
}

void Registration::sortStudents() {
    if (studentList.size()>1) {
        
        //Using Insertion sort
        for (int i = 1; i < studentList.size(); i++) {
            
            int j = i; // initialize location to place element
            
            Student x = studentList[i]; //store the value in the current element
            Student y = studentList[j-1];
            
            while(j>0 && y.compareTo(x)>0) {
                // shift element one slot to the right
                studentList[j] = y;
                j--;
            }
            studentList[j] = x; //place inserted element into the array
        }
    }
}

int Registration::courseExists(string courseName, string days, string time) {
    int index = -1;
    if (courseList.size() > 0) {
        for (int i = 0; i < courseList.size(); i++) {
            Course c = courseList[i];
            if (c.getCourseName() == courseName)
                if (c.getSchedule().getDays() == days)
                    if (c.getSchedule().getTime() == time)
                        index = i;
        }
    }
    return index;
}

bool Registration::addCourse(string courseName, int semHours, string days, string time) {
    Course c = Course(courseName, semHours, days, time);
    int i = courseExists(c.getCourseName(), c.getSchedule().getDays(), c.getSchedule().getTime());
    if (i==-1) {
        courseList.push_back(c);
        return true;
    }
    else
        return false;
}

bool Registration::removeCourse(string courseName, string days, string time) {
    int i = courseExists(courseName, days, time);
    if (i == -1)
        return false;
    else {
        courseList.erase(courseList.begin()+i);
        return true;
    }
}

string Registration::listCourses() {
    string result = "";
    if(courseList.empty())
        return ("no course\n");
    else {
        for (int i = 0; i < courseList.size(); i++) {
            Course c = courseList[i];
            result = result + c.toString();
        }
        return result;
    }
}

void Registration::sortCourses() {
    if (courseList.size()>1) {
        
        //Using Insertion sort
        for (int i = 1; i < courseList.size(); i++) {
            
            int j = i; // initialize location to place element
            
            Course x = courseList[i]; //store the value in the current element
            Course y = courseList[j-1];
            
            while(j>0 && y.compareTo(x)>0) {
                // shift element one slot to the right
                courseList[j] = y;
                j--;
            }
            courseList[j] = x; //place inserted element into the array
        }
    }
}

bool Registration::enrollCourse(string id, string courseName, string days, string time) {
    int i = studentExists(id);
    int j = courseExists(courseName, days, time);
    if (i==-1 || j==-1)
        return false;
    else {
        //if student's attempt to enroll course is successful, increment number of students in course
        if (studentList[i].enrollCourse(courseList[j]))
            courseList[j].setStudentsNum(courseList[j].getStudentsNum() + 1);
        return true;
    }
}

bool Registration::cancelCourse(string id, string courseName, string days, string time) {
    int i = studentExists(id);
    int j = courseExists(courseName, days, time);
    if (i==-1 || j==-1)
        return false;
    else {
        //if student's attempt to cancel course is successful, decrement number of students in course
        if (studentList[i].cancelCourse(courseList[j]))
            courseList[j].setStudentsNum(courseList[j].getStudentsNum() - 1);
        return true;
    }
}

void Registration::closeRegistration() {
    studentList.clear();
    courseList.clear();
}
